package com.house.keeper.service.impl;

import com.house.keeper.entity.Parking;
import com.house.keeper.entity.vo.ParkingVo;
import com.house.keeper.mapper.ParkingMapper;
import com.house.keeper.service.ParkingService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.house.keeper.base.Result;
import com.house.keeper.base.ResultCode;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zjj
 * @date 2022-06-01 08:42:43
 *
 * @description 
 */
@Service
public class ParkingServiceImpl implements ParkingService {
    @Autowired
    private ParkingMapper parkingMapper;

    /**
     * 分页查询
     *
     * @param parkingVo 查询条件
     * @return
     */
    @Override
    public Result selectByPage(ParkingVo parkingVo) {
        PageHelper.startPage(parkingVo.getPage(),parkingVo.getLimit());

        QueryWrapper<Parking> wrapper = new QueryWrapper<>();


        List<Parking> parkingList = parkingMapper.selectList(wrapper);

        PageInfo<Parking> pageInfo = new PageInfo<>(parkingList);
        return Result.success(pageInfo.getList(), pageInfo.getTotal());
    }

    /**
     * 删除
     *
     * @param id 主键
     * @return
     */
    @Override
    public Result delete(Integer id) {
        int row = parkingMapper.deleteById(id);
        if(row<=0){
            return Result.failure(ResultCode.PARAM_IS_INVALID);
        }
        return Result.success();
    }

    /**
     * 添加或修改
     *
     *  回滚：一般抛出的是RuntimeExcept异常
     *  在spring框架下，所有的异常都被org.springframework重写为RuntimeExcept
     * @param parking
     * @return
     */
    @Override
    @Transactional
    public Result update(Parking parking) {
        if(parking.getId()!=null){

            int row=parkingMapper.updateById(parking);
            if(row<=0){
                return Result.failure(ResultCode.PARAM_IS_INVALID);
            }
        }else{
//            添加
            int row =parkingMapper.insert(parking);
            if(row<=0){
                return Result.failure(ResultCode.PARAM_IS_INVALID);
            }
        }
        return Result.success();
    }

}

